<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | DebugService.php v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018/11/1
// +-------------------------------------------------------------------------------

namespace app\core\service;


use app\core\model\EnterpriseSiteConfig;
use app\core\model\SiteConfig;
use think\App;
use think\Debug;

class DebugService {
    /**
     * 检测是否需要开启debug模式
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function detectDebugMode() {
        if (App::$debug) {
            //调试模式已经打开了
            return;
        }

        $debugOpen = false;
        $openDebugFlag = session(SESSION_OPEN_DEBUG);
        if ($openDebugFlag) {
            //手动设置打开调试模式
            $debugOpen = true;
//        } else if (session((SESSION_CORE_SITE_ADMIN))) {
//            //站点管理员
//            $debugOpen = true;
        } else {
            //判断企业的调试模式有没有开启
            $enterpriseUser = session(SESSION_CORE_ENTERPRISE_USER);
            $eid = fetch($enterpriseUser, "enterprise_id");
            if ($eid) {
                $enterpriseDebug = EnterpriseSiteConfig::get($eid, SiteConfig::DEBUG);
                if ($enterpriseDebug == 'yes') {
                    $debugOpen = true;
                }
            }
        }
        if ($debugOpen) {
            //这边得手动记录一下behavior_start，否则会出异常
            Debug::remark('behavior_start', 'time');
            App::$debug = true;
        }
    }
}